home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_dates2.arc / DAYS.C < prev    next >
Text File  |  1991-01-10  |  4KB  |  130 lines

  1. /************************************************************************/
  2. /*  (c)  1987 by James N. Seed,  Atlanta, GA  -  all rights reserved.    */
  3. /*                                    */
  4. /*  This program is provided for example purposes only.  It, and/or    */
  5. /*  its executable equivalent, may not be sold or distributed in any    */
  6. /*  form for profit, without the prior written consent of the author.    */
  7. /************************************************************************/
  8.  
  9. #define     LINT_ARGS
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <c_dates.h>
  15.  
  16. static    char errtyp1[] = "\nerror:  cannot interpret the %s in the %s date.\n";
  17. static    char errtyp2[] = "\nerror:  %s date is not a valid date.\n";
  18.  
  19. static    char *fldnme[3] = { "month", "day", "year" };    /* this is for the */
  20. static    char *dtenme[2] = { "first", "second" };    /* error output.   */
  21.  
  22. void _setenvp()    { }
  23.  
  24. void main( argc, argv )
  25.  
  26. int      argc;
  27. char    **argv;
  28.  
  29. {
  30.     unsigned int    gregdte[3], i;        /* m, d, & y ( & index ) */
  31.     unsigned long    julian[2];        /* holds 2 julian dates  */
  32.     char        dtestr[29];        /* to hold verbose dates */
  33.     char        *nxtfld;        /* for dividing parms     */
  34.     unsigned int    chkdte[3];        /* used to check date     */
  35.  
  36.     if ( argc-- != 3 )            /* two parms input ? */
  37.     {
  38.          printf( "\n DAYS   is a program that calculates the number of days that" );
  39.          printf( "\n ====   exist between any two dates.\n" );
  40.          printf( "\nusage:  DAYS date1 date2" );
  41.          printf( "\n\nwhere:" );
  42.          printf( "\n\tdate1 is the starting date of the period, and" );
  43.          printf( "\n\tdate2 is the ending   date of the period." );
  44.          printf( "\n\n- Both dates MUST be input in the \"month/day/year\" format.\n" );
  45.  
  46.          exit( 0 );
  47.     }
  48.  
  49.     /*******************************************************************/
  50.     /* The following loop converts the two input dates to julian longs */
  51.     /*******************************************************************/
  52.  
  53.     while ( argc )                    /* for both dates, */
  54.     {
  55.         for ( i = 0; i < 2; ++i )        /* pick off m & d  */
  56.         {
  57.               if ( gregdte[i] = atoi( argv[argc] ) )
  58.               {
  59.                if ( nxtfld = strstr( argv[argc], "/" ) )
  60.                {
  61.                 argv[argc] = ++nxtfld;
  62.                }
  63.                else
  64.                {
  65.                 printf( errtyp1, fldnme[++i], dtenme[--argc] );
  66.                 exit( 1 );
  67.                }
  68.               }
  69.               else
  70.               {
  71.                printf( errtyp1, fldnme[i], dtenme[--argc] );
  72.                exit( 1 );
  73.               }
  74.         }
  75.  
  76.         gregdte[i] = atoi( argv[argc] );    /* then pick off y */
  77.  
  78.         /*********************************************************/
  79.         /*   this line converts the m, d, & y to a JULIAN date   */
  80.         /*********************************************************/
  81.  
  82.         julian[--argc] = gtoj( gregdte[0], gregdte[1], gregdte[2] );
  83.  
  84.         /*****************************************************/
  85.         /*   these lines check to see if the date is valid   */
  86.         /*****************************************************/
  87.  
  88.         jtog( julian[argc], (char *)chkdte, 0 );
  89.  
  90.         for ( i = 0; i < 3; ++i )
  91.         {
  92.               if ( chkdte[i] != gregdte[i] )
  93.               {
  94.                printf( errtyp2, dtenme[argc] );
  95.                exit( 1 );
  96.               }
  97.         }
  98.     }
  99.  
  100.     /*******************************************************************/
  101.     /*   this line computes the NUMBER OF DAYS between the two dates   */
  102.     /*******************************************************************/
  103.  
  104.     printf( "\nThere are %li days between the two dates,",
  105.          daycnt( *julian, julian[1], 0 ) );
  106.  
  107.     /******************************************************/
  108.     /*   these lines create the VERBOSE GREGORIAN dates   */
  109.     /******************************************************/
  110.  
  111.     fulldte( julian[0], dtestr );
  112.  
  113.     printf( "\n\n\t  %s", dtestr );
  114.  
  115.     fulldte( julian[1], dtestr );
  116.  
  117.     printf( " and %s.", dtestr );
  118.  
  119.     /*******************************************************************/
  120.     /*   this line computes the weekday, Saturday, and Sunday counts   */
  121.     /*******************************************************************/
  122.  
  123.     printf( "\n\nOf these, %li are weekdays, %li are Saturdays, and %li are Sundays.\n",
  124.          daycnt( *julian, julian[1], 2 ),
  125.          satcnt( *julian, julian[1]    ),
  126.          suncnt( *julian, julian[1]    )  );
  127.  
  128.     exit( 0 );
  129. }
  130.